package com.pactera.madp.cp.controller.oms.marketing;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.marketing.MarketingWordCreateDTO;
import com.pactera.madp.cp.api.dto.marketing.MarketingWordUpdateDTO;
import com.pactera.madp.cp.api.entity.BaseEntity;
import com.pactera.madp.cp.api.entity.marketing.MarketingWordEntity;
import com.pactera.madp.cp.api.vo.marketing.MarketingWordVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.MarketingWordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * (McMarketingWord)表控制层
 *
 * @author yushen.qi
 * @since 2021-05-24 18:56:04
 */
@RestController
@RequestMapping("/marketing-word")
@Api(value = "marketingWord", tags = "营销素材-文档管理")
public class MarketingWordController {

    /**
     * 服务对象
     */
    @Resource
    private MarketingWordService marketingWordService;

    /**
     * 模糊查询
     *
     * @param title
     * @param classification
     * @return
     */
    @GetMapping("/index")
    @ApiOperation(value = "根据名称或分页查询")
    public R<List<MarketingWordVO>> qryPage(String title, Integer classification) {
        return R.ok(marketingWordService.selectAllOrName(title,classification));
    }

    /**
     * 新增
     *
     * @param dto
     * @return
     */
    @SysLog("新增营销素材-文档")
    @PostMapping
    @ApiOperation(value = "新增")
    public R<Map<String, Integer>> saveArticle(@RequestBody MarketingWordCreateDTO dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        MarketingWordEntity one = marketingWordService.getOne(Wrappers.<MarketingWordEntity>lambdaQuery().eq(MarketingWordEntity::getTitle, dto.getTitle()).eq(BaseEntity::getDelFlag,0).eq(MarketingWordEntity::getCorpId,corpId).eq(MarketingWordEntity::getClassification,dto.getClassification()));
        if(one!=null){
            return R.failed("标题不能重复！");
        }
        return R.ok(marketingWordService.insert(dto));
    }

    /**
     * 编辑
     *
     * @param wordDto
     * @return
     */
    @SysLog("编辑营销素材-文档")
    @PutMapping
    @ApiOperation(value = "编辑")
    public R<Boolean> putArticle(@RequestBody MarketingWordUpdateDTO wordDto) {
        return marketingWordService.update(wordDto)?R.ok():R.failed("修改失败");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @SysLog("删除营销素材-文档")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public R<Boolean> delete(@PathVariable Integer id) {
        return  marketingWordService.remove(id);
    }

    /**
     * 发布文档
     *
     * @param id
     * @return
     */
    @SysLog("发布营销素材-文档")
    @GetMapping("/release/{id}")
    @ApiOperation(value = "发布文档")
    public R<Boolean> releaseExcel(@PathVariable Integer id){
        MarketingWordEntity marketingWordEntity = new MarketingWordEntity();
        marketingWordEntity.setMarketingState(2);
        marketingWordEntity.setUpdateTime(LocalDateTime.now());
        return marketingWordService.update(marketingWordEntity, Wrappers.<MarketingWordEntity>lambdaQuery().eq(MarketingWordEntity::getId,id))?R.ok(null,"发布成功"):R.failed("发布失败");
    }

}
